package com.gpframework.module.controller.purchase;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.purchase.domain.GpPurchaseColourComparison;
import com.gpframework.module.purchase.domain.req.GpPurchaseColourComparisonAdd;
import com.gpframework.module.purchase.domain.req.GpPurchaseColourComparisonReq;
import com.gpframework.module.purchase.domain.req.GpPurchaseColourComparisonUpdate;
import com.gpframework.module.purchase.domain.vo.GpPurchaseColourComparisonVo;
import com.gpframework.module.purchase.service.IGpPurchaseColourComparisonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName GpPurchaseColourComparisonController
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 采购颜色对照基础Controller
 */
@Validated
@Api(tags = "采购颜色对照基础")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/purchase/colourComparison" )
public class GpPurchaseColourComparisonController extends BaseController {

    private final IGpPurchaseColourComparisonService iGpPurchaseColourComparisonService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-09
  * @Param gpPurchaseColourComparison
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:list')")
    @GetMapping("/list")
    public TableDataInfo<GpPurchaseColourComparisonVo> list(GpPurchaseColourComparisonReq gpPurchaseColourComparisonReq)
    {
        startPage();
        QueryWrapper<GpPurchaseColourComparison> lqw = new QueryWrapper<GpPurchaseColourComparison>();
        gpPurchaseColourComparisonReq.generatorQuery(lqw,true);
        List<GpPurchaseColourComparisonVo> list = iGpPurchaseColourComparisonService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseColourComparison
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:add')" )
    @Log(title = "添加采购颜色对照基础" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpPurchaseColourComparisonAdd gpPurchaseColourComparisonAdd) {
        GpPurchaseColourComparison gpPurchaseColourComparison = Convert.convert(new TypeReference<GpPurchaseColourComparison>() {}, gpPurchaseColourComparisonAdd);
        return toAjax(iGpPurchaseColourComparisonService.save(gpPurchaseColourComparison) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseColourComparison
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:edit')" )
    @Log(title = "修改采购颜色对照基础" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpPurchaseColourComparisonUpdate gpPurchaseColourComparisonUpdate) {
        GpPurchaseColourComparison gpPurchaseColourComparison = Convert.convert(new TypeReference<GpPurchaseColourComparison>() {}, gpPurchaseColourComparisonUpdate);
        return toAjax(iGpPurchaseColourComparisonService.updateById(gpPurchaseColourComparison) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseColourComparison
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:export')" )
    @Log(title = "导出采购颜色对照基础" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpPurchaseColourComparisonReq gpPurchaseColourComparisonReq) {
        QueryWrapper<GpPurchaseColourComparison> lqw = new QueryWrapper<GpPurchaseColourComparison>();
        gpPurchaseColourComparisonReq.generatorQuery(lqw,true);
        List<GpPurchaseColourComparisonVo> list = iGpPurchaseColourComparisonService.listVo(lqw);
        ExcelUtil<GpPurchaseColourComparisonVo> util = new ExcelUtil<GpPurchaseColourComparisonVo>(GpPurchaseColourComparisonVo. class);
        return util.exportExcel(list, "colourComparison" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseColourComparison
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpPurchaseColourComparisonVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpPurchaseColourComparisonService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseColourComparison
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:remove')" )
    @Log(title = "删除采购颜色对照基础" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpPurchaseColourComparisonService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("下载模板")
    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:export')" )
    @Log(title = "下载模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult importTemplate() {
        ExcelUtil<GpPurchaseColourComparisonVo> util = new ExcelUtil<GpPurchaseColourComparisonVo>(GpPurchaseColourComparisonVo.class);
        return util.importTemplateExcel("欧根纱颜色对照基础表");
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("导入")
    @PreAuthorize("@ss.hasPermi('purchase:colourComparison:add')" )
    @Log(title = "导入" , businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importList(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }
        // 删除旧数据
        iGpPurchaseColourComparisonService.remove(new QueryWrapper<GpPurchaseColourComparison>().eq("del_status", YesNoEnum.NO.getKey()));
        // 导入新数据
        ExcelUtil<GpPurchaseColourComparisonVo> util = new ExcelUtil<>(GpPurchaseColourComparisonVo.class);
        List<GpPurchaseColourComparisonVo> excelList = util.importCacheExcel(file.getInputStream());
        List<GpPurchaseColourComparison> list = Convert.convert(new TypeReference<List<GpPurchaseColourComparison>>() {}, excelList);
        return iGpPurchaseColourComparisonService.saveBatch(list) ? AjaxResult.success("导入成功") : AjaxResult.error();
    }
}
